<%@ page language="java" import="java.util.*,com.scm.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>用户管理</title>

<meta http-equiv="Content-Type" content="text/html; charset=utf8">
<script type="text/javascript" src="script/jquery-1.8.1.min.js"></script>
<script type="text/javascript" src="<%=path%>/script/My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript" src="script/common.js"></script>
<script type="text/javascript" src="script/productDiv.js"></script>
<link href="css/style.css" rel="stylesheet" type="text/css">
<script type="text/javascript">
		var flag = true;//定义一个 标记变量
		//分页查询
		function goPage(currentPage){
			var account = $.trim($("#account").val());
			var name = $.trim($("#name").val());
			var startDate = $.trim($("#startDate").val());
			var endDate = $.trim($("#endDate").val());
			//什么 都没有选中
			var status = -1;
			if($("input[name=status]:checked").length == 1){
				status = $("input[name=status]:checked").val();
			}
			/* var params ={"currentPage":currentPage,"account":account,"name":name,"startDate":startDate,"endDate":endDate,"status":status};
			$("#dataList").load("main/system/scmuserShow",params);  */
			window.location.href = "<%=path%>/main/system/scmuserShow?currentPage="+currentPage+"&account="+account+"&name="+name+"&startDate="+startDate+"&endDate="+endDate+"&status="+status;
		}
		
		
		//查看用户权限
		function showModels(account){
			if(flag){
				flag = false;
				$("body>div").hide();
				$("#account_").text(account);
				
				var xmlHttp = new XMLHttpRequest();
				xmlHttp.open("get", "main/system/usermodelShows?account="+account , true);
				//发送请求
				xmlHttp.send();
				//在状态发生改变时调用的函数
				xmlHttp.onreadystatechange = function(){
					//响应完成时执行
					if(xmlHttp.status == 200 && xmlHttp.readyState ==4){
						var userModels = $("#userModels");
						userModels.html("");
						var models = JSON.parse(xmlHttp.responseText);
						for(var i=0;i<models.length;i++){
							userModels.append("["+models[i].modelName+" ").append(" ").append("]");
						}
					}
					$("#models").show();
					flag = true;
				}
			}
		}
		//返回scmuser列表首页
		function backScmuser(){
			$("body>div").hide();
			$("#u").show();
		}
		//打开新增用户层
		function openAdd(){
			if(flag){
				flag = false;
				$("body>div").hide();
				
				//初始化窗口内容
				$("#new_account,#new_name,#password").val("");
				$("#optTab tr").find("td:eq(2)").text("");//错误信息显示空白
				$(":input:radio[value=0]").attr("checked",true);
				$(":input[name=modelCode]").attr("checked",false);
				$("#HideTitle").text("新增");
				
				var xmlHttp = new XMLHttpRequest();
				xmlHttp.open("get","main/system/systemmodels",true);
				xmlHttp.send();
					
				xmlHttp.onreadystatechange = function(){
					if(xmlHttp.status == 200 && xmlHttp.readyState == 4){
						var sys = $("#systemModels");
						sys.html("");
						var m = JSON.parse(xmlHttp.responseText);
						for(var i=0;i<m.length;i++){
							sys.append("<input type='checkbox' name='modelCode'  value='"+m[i].modelCode+"'/>").append(m[i].modelName).append(" ");
						}
						$("#add").show();
						flag = true;
					}
				}
			}
		}
		//保存用户
		function saveScmuser(btn){
			var b = true;//标记验证通过
			var hidTitel = $("#HideTitle").text();
			
			var account = $.trim($("#new_account").val());
			var name = $.trim($("#new_name").val());
			var password = $.trim($("#new_password").val());
			var status = $("input[name=new_status]:checked").val();
			
			var modelCode = $("input[name=modelCode]:checked");
			if(account == ""){
				$("#accountInfo").text("用户账号不能为空");//用户账号不能为空
				b = false;
			}else{
				$("#accountInfo").text("");
			}
			if(name == ""){
				$("#nameInfo").text("用户姓名不能为空");//用户姓名不能为空
				b = false;
			}else{
				$("#nameInfo").text("");
			}
			
			if(modelCode.length == 0){
				$("#modelInfo").text("请选择用户权限");
				b = false;
			}else{
				$("#modelInfo").text();
			}
			//验证通过
			if(b){
				//请求参数
				var params = "account="+account+"&name="+name+"&password="+password+"&status="+status;
				modelCode.each(function(){
					params += "&modelCode="+$(this).val();
				});
				
				var req = new XMLHttpRequest();
				
				if(hidTitel == "新增"){
					req.open("get","<%=path%>/main/system/scmuserAdd?"+ params,false);
					req.send();
					
					var r = req.responseText;
					if(r == "ok"){
						alert("添加成功！");
						window.location.reload();//重新加载页面
					}else if(r == "duplicate"){
						$("#accountInfo").text("账户已经存在！");
					}else {
						$("#accountInfo").text("添加失败！");
					}
				}else if(hidTitel == "修改"){
					if(confirm("确认要修改吗？")){
						req.open("get","<%=path%>/main/system/scmuserUpdate?" + params,false);
						req.send();
						
						var r = req.responseText;
						if(r == "ok"){
							alert("修改成功！");
							window.location.reload();//重新加载页面
						}else{
							$("#accountInfo").text("修改失败！");
						}
					}
				}
			}
		}
		
		//删除用户
		function del(account){
			if(confirm("确认要删除吗？")){
				var req = new XMLHttpRequest();
				req.open("get","<%=path%>/main/system/scmuserDelete?account=" + account, false);
				req.send();
				var r = req.responseText;
				if (r == "ok") {
					alert("删除成功！");
					window.location.reload();
				} else if (r == "fail") {
					alert("删除失败！");
				}
			}
		}

	//打开修改显示页面
	function upd(account) {
		if (flag) {
			flag = false;
			$("body>div").hide();

			//初始化窗口内容
			$("#optTab tr").find("td:eq(2)").text("");//错误信息显示空白
			$("#HideTitle").text("修改");

			var xmlHttp = new XMLHttpRequest();
			xmlHttp.open("get", "main/system/scmuserupdateinfo?account=" + account, true);
			xmlHttp.send();

			xmlHttp.onreadystatechange = function() {
				if (xmlHttp.status == 200 && xmlHttp.readyState == 4) {
					var sys = $("#systemModels");
					sys.html("");
					var r = JSON.parse(xmlHttp.responseText);

					var user = r.scmuser;//用户信息
					var m = r.models;//权限信息
					//显示用户信息
					$("#new_account,#new_name,#password").val("");
					$("#new_account").attr("readonly", true);
					$("#new_account").val(user.account);
					$("#new_name").val(user.name);
					$("#new_password").val(user.password);
					$("input[name=new_status][value=" + user.status + "]").attr("checked", true);
					//显示所有权限
					for (var i = 0; i < m.length; i++) {
						sys.append("<input type='checkbox' name='modelCode'  value='"+m[i].modelCode+"'/>").append(m[i].modelName).append(" ");
					}
					//选中自己已有的权限
					for (var i = 0; i < user.models.length; i++) {
						//自己权限选中
						$("input[name=modelCode][value="+ user.models[i].modelCode + "]").attr("checked", true);
					}
					$("#add").show();
					flag = true;
				}
			}
		}
	}
	
</script>
</head>

<body>
	<div id="u">
		<table width="100%" border="0" cellpadding="0" cellspacing="0">
			<tr>
				<td class="title1">您的位置:系统管理--用户管理</td>
			</tr>
		</table>
		<table width="100%" border="0" cellpadding="0" cellspacing="0">
			<tr>
				<td width="30px" class="toolbar">&nbsp;</td>
				<td width="40px" class="toolbar" onMouseOver="OMO(event)"
					onMouseOut="OMOU(event)" onClick="openAdd()"><img src="images/new.gif">新增</td>
				<td width="80%" class="toolbar">&nbsp;</td>
				<td class="toolbar">&nbsp;当前用户: &nbsp;<span style='color: blue;font-weight: bolder '>${scmuser.name }</span></td>
			</tr>
		</table>
		<%
			//查询条件
			String account = request.getParameter("account");
			String name = request.getParameter("name");
			String startDate = request.getParameter("startDate");
			String endDate = request.getParameter("endDate");
			String status = request.getParameter("status");

			account = account == null ? "" : account;
			name = name == null ? "" : name;
			startDate = startDate == null ? "" : startDate;
			endDate = endDate == null ? "" : endDate;
			status = status == null ? "" : status;
		%>
		<div class="queryDiv">
			用户账号：<input type="text" id="account" value="<%=account%>" /> 
			用户姓名：<input type="text" id="name" value="<%=name%>" /> 
			添加日期： <input class="Wdate" type="text" id="startDate" value="<%=startDate%>" onFocus="WdatePicker({isShowClear:true,readOnly:true})" /> 
					- 
					<input class="Wdate" type="text" id="endDate" value="<%=endDate%>" onFocus="WdatePicker({isShowClear:true,readOnly:true})" /> 
			锁定状态：
			<%
			if ("1".equals(status)) {
				out.print("<input type='radio' name='status' value='0'/>不锁定");
				out.print("<input type='radio' name='status' value='1' checked='checked'/>锁定");
			} else if ("0".equals(status)) {
				out.print("<input type='radio' name='status' value='0' checked='checked'/>不锁定");
				out.print("<input type='radio' name='status' value='1' />锁定");
			} else {
				out.print("<input type='radio' name='status' value='0' />不锁定");
				out.print("<input type='radio' name='status' value='1' />锁定");
			}
			%>
			<input type="button" value="查询" onclick="goPage(1)" />
		</div>
		<table width="100%" border="0" align="center" cellspacing="1"
			class="c">
			<tr>
				<td class="title1">用户账号</td>
				<td class="title1">用户姓名</td>
				<td class="title1">添加日期</td>
				<td class="title1">锁定状态</td>
				<td class="title1">权限</td>
				<td class="title1">操作</td>
			</tr>
			<c:forEach items="${page.dataList }" var="user">
				<tr>
					<td align="center">${user.account }</td>
					<td align="center">${user.name }</td>
					<td align="center">${user.createDate }</td>
					<td align="center">
						<c:if test="${user.status ==0 }">不锁定</c:if>
						<c:if test="${user.status ==1 }">锁定</c:if>
					</td>
					<td align="center"><input type="button" value="查看权限" onclick="showModels('${user.account }')" /></td>
					<td align="center">
						<a href="javascript:upd('${user.account }')">修改</a> 
						<a href="javascript:del('${user.account }')">删除</a>
					</td>
				</tr>
			</c:forEach>
		</table>
		<!-- 导入分页层 -->
		<%@include file="../../public/page.jsp"%>
	</div>
	<!-- 查看权限层 -->
	<div id="models" class="hidd">
		<table width="100%" border="0" cellpadding="0" cellspacing="0">
			<tr>
				<td class="title1">您的位置：系统管理--用户管理--查看<span id="account_"></span>权限
				</td>
			</tr>
		</table>

		<div id="userModels" class="pageDiv"></div>
		<div class="pageDiv">
			<input type="button" value="返回" onclick="backScmuser()" />
		</div>
	</div>
	<!-- 新增或者删改用户层 -->
	<div id="add" class="hidd">
		<table width="100%" border="0" cellpadding="0" cellspacing="0">
			<tr>
				<td nowrap class="title1">您的位置：系统管理－－用户管理－－<span id="HideTitle"></span>用户</td>
			</tr>
		</table>
		<table width="100%" border="0" align="center" cellspacing="1" class="c" id="optTab">
			<tr>
				<td align="center">用户账号</td>
				<td align="left"><input type="text" id="new_account" /></td>
				<td width="30%" id="accountInfo" class="wrong"></td>
			</tr>
			<tr>
				<td align="center">用户姓名</td>
				<td align="left"><input type="text" id="new_name" /></td>
				<td width="30%" id="nameInfo" class="wrong"></td>
			</tr>
			<tr>
				<td align="center">密码</td>
				<td align="left"><input type="password" id="new_password" /></td>
				<td width="30%" id="passwordInfo" class="wrong"></td>
			</tr>
			<tr>
				<td align="center">锁定状态</td>
				<td align="left">
				<input type="radio" name="new_status" value="0" checked="checked" />否
				<input type="radio" name="new_status" value="1" />是</td>
				<td width="30%" id="statusInfo" class="wrong"></td>
			</tr>
			<tr>
				<td align="center">用户权限</td>
				<td align="left" id="systemModels"></td>
				<td align="left" id="modelInfo" class="wrong"></td>
			</tr>
			<tr>
				<td height="18" colspan="3" align="center">
				<input type="button" value="保存" onclick="saveScmuser(this)" />
				<input type="button" value="取消" onclick="backScmuser()" /></td>
			</tr>
		</table>
	</div>
	<div id="dataList"></div>
	
</body>
</html>
